package club.beimeng.eduservice.service.impl;

import club.beimeng.eduservice.entity.EduChapter;
import club.beimeng.eduservice.entity.EduVideo;
import club.beimeng.eduservice.entity.chapter.ChapterVo;
import club.beimeng.eduservice.entity.chapter.VideoVo;
import club.beimeng.eduservice.mapper.EduChapterMapper;
import club.beimeng.eduservice.service.EduChapterService;
import club.beimeng.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseID(String courseId) {
        // 1 根据课程id查询出所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        wrapperChapter.orderByAsc("sort");
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);
        // 2 根据课程id查询所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId).orderByAsc("sort");
        List<EduVideo> eduVideos = videoService.list(wrapperVideo);

        ArrayList<ChapterVo> answerList = new ArrayList<>(); //最终返回结果
        // 3 根据章节查询小节
        eduChapters.forEach((eduChapter -> {
            String chapterId = eduChapter.getId();
            ChapterVo chapterVo = new ChapterVo();
            List<VideoVo> children = chapterVo.getChildren();
            eduVideos.forEach((eduVideo -> {
                if (eduVideo.getChapterId().equals(chapterId)){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    children.add(videoVo);
                }
            }));
            BeanUtils.copyProperties(eduChapter,chapterVo);
            chapterVo.setChildren(children);
            // 4 获得所有数据进行封装
            answerList.add(chapterVo);
        }));
        return answerList;
    }

    @Override
    public boolean removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        int count = baseMapper.delete(queryWrapper);
        return count > 0;
    }

    @Override
    public Boolean deleteChapter(String chapterId) {
        // 根据章节id查询小节表，如果查询数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        //判断
        if (count > 0){ //不进行删除
            throw new RuntimeException("包含小节");
        }else {
            int result = baseMapper.deleteById(chapterId);
            return true;
        }
    }
}
